<template>
  <div class="app-container app-content">
    <el-row>
      <el-col :span="24">
        <el-row class="line-bottom" v-show="showSearch">
          <el-col class="form_col">
            <el-form :model="queryParams" ref="queryForm" :size="size" v-show="showSearch" :label-width="labelWidth">
              <div id="searchFilter" :gutter="10" style="display: flex; flex-wrap: wrap">
                <el-form-item label="协议名称" prop="protocolName">
                  <el-input
                      v-model="queryParams.protocolName"
                      placeholder="请输入协议名称"
                      clearable
                      @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
                <el-form-item label="厂家名称" prop="supplierId">
                  <el-select v-model="queryParams.supplierId" placeholder="请选择厂家名称">
                    <el-option v-for="dict in dict.type.terminal_supplier"
                               :key="dict.value"
                               :label="dict.label"
                               :value="dict.value.toString()"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="设备类型" prop="terminalType">
                  <el-select v-model="queryParams.terminalType" placeholder="请选择设备类型" style="width: 100%"
                             clearable>
                    <el-option v-for="dict in dict.type.terminal_meter_type"
                               :key="dict.value"
                               :label="dict.label"
                               :value="dict.value"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item style="width: 0px">
                </el-form-item>
              </div>
            </el-form>
          </el-col>
          <el-col class="form_query">
            <el-button v-show="collapseAble" type="text" @click="shiftCollapseAble" class="text">
              <span>
                {{ fold ? '收起筛选' : '更多筛选' }}
              </span>
            </el-button>
            <el-button type="primary" @click="handleQuery" size="mini">查询</el-button>
            <el-button @click="resetQuery" size="mini">重置</el-button>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['basedata:protocol:add']"
            >新增
            </el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['basedata:protocol:edit']"
            >修改</el-button>
          </el-col> -->
          <el-col :span="1.5">
            <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['basedata:protocol:remove']"
            >批量删除
            </el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['basedata:protocol:export']"
            >导出</el-button>
          </el-col> -->
          <RightDropdown :columns="columns" :showSearch.sync="showSearch" @queryTable="getList" @export="handleExport"
                         :hasPermiExport="['basedata:protocol:export']"></RightDropdown>
          <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
        </el-row>

        <el-table v-loading="loading" :data="protocolList" border size="mini" :height="getAvailableHeight"
                  @selection-change="handleSelectionChange" ref="table">
          <el-table-column type="selection" width="55" align="center"/>
          <!-- <el-table-column label="id" align="center" prop="id" /> -->
          <el-table-column label="协议编号" align="center" prop="protocolId" v-if="columns[0].visible"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="协议名称" align="center" prop="protocolName" v-if="columns[1].visible"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="厂家名称" align="center" prop="supplierId" v-if="columns[2].visible"
                           :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.terminal_supplier" :value="scope.row.supplierId"/>
            </template>
          </el-table-column>
          <el-table-column label="方法名" align="center" prop="methodName" v-if="columns[3].visible"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="类名" align="center" prop="className" v-if="columns[4].visible"
                           :show-overflow-tooltip="true" width="350"/>
          <el-table-column label="备注" align="center" prop="remark" v-if="columns[5].visible"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="设备类型" align="center" prop="terminalType" v-if="columns[6].visible"
                           :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.terminal_meter_type" :value="parseInt(scope.row.terminalType)"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['basedata:protocol:edit']"
              >修改
              </el-button>
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['basedata:protocol:remove']"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
      </el-col>
    </el-row>


    <!-- 添加或修改协议对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="660px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :inline="true">
        <el-form-item label="协议编号" prop="protocolId">
          <el-input v-model="form.protocolId" placeholder="请输入协议编号"/>
        </el-form-item>
        <el-form-item label="协议名称" prop="protocolName">
          <el-input v-model="form.protocolName" placeholder="请输入协议名称"/>
        </el-form-item>
        <el-form-item label="厂家名称" prop="supplierId">
          <el-select v-model="form.supplierId" placeholder="请选择厂家名称" style="width:202px">
            <el-option v-for="dict in dict.type.terminal_supplier"
                       :key="dict.value"
                       :label="dict.label"
                       :value="dict.value"></el-option>
          </el-select>
<!--          <el-input v-model="form.supplierId" placeholder="请输入厂家编号"/>-->
        </el-form-item>
        <el-form-item label="方法名" prop="methodName">
          <el-input v-model="form.methodName" placeholder="请输入方法名"/>
        </el-form-item>
        <el-form-item label="类名" prop="className">
          <el-input v-model="form.className" placeholder="请输入类名"/>
        </el-form-item>
        <el-form-item label="设备类型" prop="terminalType">
          <el-select v-model="form.terminalType" placeholder="请选择设备类型" style="width:202px">
            <el-option v-for="dict in dict.type.terminal_meter_type"
                       :key="dict.value"
                       :label="dict.label"
                       :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listProtocol, getProtocol, delProtocol, addProtocol, updateProtocol} from "@/api/basedata/protocol";

export default {
  name: "Protocol",
  dicts:['terminal_meter_type', 'terminal_supplier'],
  data() {
    return {

      screenHeight: null,
      size: 'mini',
      collapseAble: false,
      fold: false,
      maxShow: '5',
      labelWidth: '80px',
      // terminalType: [
      //   {label: '大用户表', value: 1},
      //   {label: '户表', value: 2}
      // ],
      // 列信息
      columns: [
        {key: 0, label: `协议编号`, visible: true},
        {key: 1, label: `协议名称`, visible: true},
        {key: 2, label: `厂家编号`, visible: true},
        {key: 3, label: `方法名`, visible: true},
        {key: 4, label: `类名`, visible: true},
        {key: 5, label: `备注`, visible: true},
        {key: 6, label: `设备类型`, visible: true},
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      protocolIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 协议表格数据
      protocolList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        protocolName: null,
        supplierId: null,
        terminalType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        protocolId: [
          {required: true, message: "协议编号不能为空", trigger: "blur"}
        ],
        supplierId: [
          {required: true, message: "厂家编号不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.screenHeight = document.body.clientHeight
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - (this.showSearch ? 245 : 200)
    },
  },
  mounted() {
    // 通过最大显示个数控制展开/折叠
    if (this.maxShow > 0) {
      this.minShowControl()
    }
  },
  methods: {
    // 展开折叠按钮
    shiftCollapseAble() {
      this.fold = !this.fold
      this.minShowControl()
    },
    // 通过maxShow控制元素显示/折叠
    minShowControl() {
      const group = window.document.querySelectorAll(`#searchFilter .el-form-item.el-form-item--${this.size}`)
      const len = group?.length ? group?.length - 1 : 0
      if (this.maxShow < len) {
        group.forEach((item, index) => {
          if (index > this.maxShow - 1 && index < len) {
            item.hidden = !this.fold
          }
        })
        this.collapseAble = true
      } else {
        this.collapseAble = false
      }
    },
    /** 查询协议列表 */
    getList() {
      this.loading = true;
      listProtocol(this.queryParams).then(response => {
        this.protocolList = response.rows;
        this.$nextTick(() => {
          window.setTimeout(() => {
            this.$refs.table.doLayout()
          }, 100)
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        protocolId: null,
        protocolName: null,
        supplierId: null,
        methodName: null,
        className: null,
        remark: null,
        terminalType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.protocolIds = selection.map(item => item.protocolId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加协议";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProtocol(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改协议";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProtocol(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProtocol(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const protocolIds = row.protocolId || this.protocolIds
      this.$modal.confirm('是否确认删除协议编号为"' + protocolIds + '"的数据项？').then(function () {
        return delProtocol(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basedata/protocol/export', {
        ...this.queryParams
      }, `协议_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.form_col {
  width: 80%;

  ::v-deep .el-form {
    width: 100%;
  }

  ::v-deep .el-form-item {
    width: 20%;
  }
}

.form_query {
  width: 20%;
  text-align: right;
}
</style>
